package com.wg.net.operations.base;

import com.wg.core.io.IBinaryReader;
import com.wg.core.io.IBinaryWriter;
import com.wg.core.io.IBinaryWritable;

public class OperationResponse {
    /**
     * 操作编号
     */
    protected short opCode;

    /**
     * 返回标识
     */
    protected short returnCode;

    /**
     * debug
     */
    protected String debugMsg;

    /**
     * bytes
     */
    private byte[] buffer;

    public OperationResponse() {
    }

    public OperationResponse(short opCode) {
        this.opCode = opCode;
    }

    public OperationResponse(short opCode, byte[] buffer) {
        this.opCode = opCode;
        this.buffer = buffer;
    }

    public OperationResponse(short opCode, IBinaryWritable contract) {
        this(opCode, contract.toBytes());
    }

    public short getOpCode() {
        return opCode;
    }

    public void setOpCode(short opCode) {
        this.opCode = opCode;
    }

    public byte[] getBuffer() {
        if (buffer == null) {
            return new byte[0];
        } else {
            return buffer;
        }
    }

    public void setBuffer(byte[] buffer) {
        this.buffer = buffer;
    }

    public short getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(short returnCode) {
        this.returnCode = returnCode;
    }

    public String getDebugMsg() {
        return debugMsg;
    }

    public void setDebugMsg(String debugMsg) {
        this.debugMsg = debugMsg;
    }

    public void read(IBinaryReader reader) {
        opCode = reader.readShort();
        returnCode = reader.readShort();
        debugMsg = reader.readString();
        buffer = reader.readRemainingBytes();
    }

    public void write(IBinaryWriter writer) {
        writer.writeShort(opCode);
        writer.writeShort(returnCode);
        writer.writeString(debugMsg);
        writer.writeBytes(buffer);
    }

    @Override
    public String toString() {
        return "responseCode=" + opCode + ", returnCode=" + returnCode + ", msg='" + debugMsg + '\'';
    }
}
